package com.example.prarranque5;

import android.graphics.DashPathEffect;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.androidplot.Plot;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYStepMode;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Random;

/**
 * Created by JuanJoseSanz on 28/07/13.
 */
public class prxyplot extends MainActivity {


    private int nPuntos , intcaca ;
    private double jIni , djIni ;
    private Number[] myArray , myArray2 , myArray3 ;
    private XYPlot dynamicPlot, staticPlot, mySimpleXYPlot;
    private LineAndPointFormatter series1Format ;
    private XYSeries series1 ;
    //    private MyPlotUpdater plotUpdater;
    private Button leftButton, rightButton ;
    private TextView textPrDynPlot ,textPrDynPlot2 ;

    private SimpleXYSeries simpleSeries1 ;

//    private class MyPlotUpdater {
//    }




    @Override
    public void onCreate(Bundle savedInstanceState) {

        // android boilerplate stuff
        super.onCreate(savedInstanceState);

        setContentView(R.layout.prdp); // initialize our XYPlot reference:


        leftButton = (Button) findViewById(R.id.leftButton) ;
        leftButton.setOnClickListener(this);

        rightButton = (Button) findViewById(R.id.rightButton) ;
        rightButton.setOnClickListener(this); ;

        textPrDynPlot = (TextView) findViewById(R.id.textPrDynPlot) ;
        textPrDynPlot2 = (TextView) findViewById(R.id.textPrDynPlot2) ;

        dynamicPlot = (XYPlot) findViewById(R.id.dynamicPlot);

//---        plotUpdater = new MyPlotUpdater(dynamicPlot);
        // only display whole numbers in domain labels
//        dynamicPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));
        // Create a couple arrays of y-values to plot:

        nPuntos = 10 ;

//        myArray3 = new Number[nPuntos];
//---            try {
//---                Thread.sleep(1000);
//---            } catch (InterruptedException ex) {}

        jIni = 0d ;
        djIni = 0.1d ;

        textPrDynPlot.setText(""+red);
//        textPrDynPlot2.setText(""+seriesBuilding2(nPuntos,jIni));
        textPrDynPlot2.setText(""+randomDouble());

        series1Format = new LineAndPointFormatter(
                null,
//                Color.rgb(0, 0, 255),                   // line color
                red,
//                Color.rgb(255, 255, 255),               // point color
                null);                                  // fill color (none)


        dynamicPlot.addSeries(seriesBuilding5(nPuntos, jIni), series1Format);

        // Plot background colour
        dynamicPlot.getGraphWidget().getGridBackgroundPaint().setColor(white);
        // Grid colour
        dynamicPlot.getGraphWidget().getGridLinePaint().setColor(white);
        dynamicPlot.getGraphWidget().getGridLinePaint().setPathEffect(new DashPathEffect(new float[]{1,1}, 1));
        dynamicPlot.getGraphWidget().getDomainOriginLinePaint().setColor(blue);
        dynamicPlot.getGraphWidget().getRangeOriginLinePaint().setColor(green);

        dynamicPlot.setBorderStyle(Plot.BorderStyle.SQUARE, null, null);
        dynamicPlot.getBorderPaint().setStrokeWidth(1);
        dynamicPlot.getBorderPaint().setAntiAlias(false);
        // External frame colour
        //        dynamicPlot.getBorderPaint().setColor(green);

        dynamicPlot.getGraphWidget().setPaddingRight(2);

        // draw a domain tick for each year:
        //dynamicPlot.setDomainStep(XYStepMode.SUBDIVIDE, 9);
        dynamicPlot.setDomainStep(XYStepMode.SUBDIVIDE, 1);
        dynamicPlot.setRangeStep(XYStepMode.SUBDIVIDE, 1);
        // Y range
        dynamicPlot.setRangeBoundaries(-3,3,BoundaryMode.FIXED);
        // X range
        dynamicPlot.setDomainBoundaries(-4,4, BoundaryMode.FIXED);
        // customize our domain/range labels
//        dynamicPlot.setDomainLabel("Frequency (Hz)");
//        dynamicPlot.setRangeLabel("Loud pressure (dB)");
        dynamicPlot.setDomainLabel("");
        dynamicPlot.setRangeLabel("");
        dynamicPlot.getLegendWidget().setVisible(false);
        // get rid of decimal points in our range labels:
        dynamicPlot.setRangeValueFormat(new DecimalFormat("0"));

        //mySimpleXYPlot.setDomainValueFormat(new MyDateFormat());

        // by default, AndroidPlot displays developer guides to aid in laying out your plot.
        // To get rid of them call disableAllMarkup():
        dynamicPlot.disableAllMarkup();


//            Number[] series1Numbers = {Math.sin(0d), Math.sin(1d), Math.sin(2d), Math.sin(3d), Math.sin(4d), Math.sin(5d)};
//            Number[] series2Numbers = {1, 2, 4, 9, 16, 25};
//        LineAndPointFormatter f1 = new LineAndPointFormatter(Color.rgb(0, 0, 200), null,
//                Color.rgb(0, 0, 80));
        // Turn the above arrays into XYSeries':
        // Create a formatter to use for drawing a series using LineAndPointRenderer:
//        dynamicPlot.addSeries(series1,
//                new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), null));
//        dynamicPlot.addSeries(myArray, f1);
//        dynamicPlot.addSeries(myArray, new LineAndPointFormatter(Color.rgb(0, 0, 0), null, Color.rgb(0, 80, 0)));
        // create a series using a formatter with some transparency applied:
//        LineAndPointFormatter f1 = new LineAndPointFormatter(Color.rgb(0, 0, 200), null, Color.rgb(0, 0, 80));
//        f1.getFillPaint().setAlpha(220);
//        dynamicPlot.addSeries(series2Numbers, f1);
//
//        dynamicPlot.setGridPadding(5, 0, 5, 0);
//---            dynamicPlot.redraw();



    }







    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    //????????????????????????????????????????
//?????????????????????????????????????????  POR QUE "onClick" TIENE QUE SER PUBLIC?????
//????????????????????????????????????????
    @Override
    public void onClick(View v) {

        if(v.getId()==R.id.leftButton)
        {
//                intcaca = 42 ;
//                textPrDynPlot.setText("RESULTADO=" + intcaca);
            // get handles to our View defined in layout.xml:
            dynamicPlot.clear();
            jIni = jIni + djIni ;
            dynamicPlot.addSeries(seriesBuilding3(nPuntos,jIni), series1Format);
            dynamicPlot.redraw();
        }



        if(v.getId()==R.id.rightButton)
        {
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException ex) {}

            dynamicPlot.clear();
            jIni = jIni - djIni ;
            dynamicPlot.addSeries(seriesBuilding3(nPuntos,jIni), series1Format);
            dynamicPlot.redraw();
        }


    }





////////////////////////////////////////////////////////////////////
//   Series of scatter plot functions
//------------------------------------------------------------------

    private XYSeries seriesBuilding1(int nP, double jI){
        Number[] array = new Number[nP];
        XYSeries series ;
        for (int j=0 ; j<=nP-1 ; j++)   {
            array[j] = Math.cos(0.1d* j + jI) ;
        }
        series = new SimpleXYSeries(
                Arrays.asList(array),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                " ");                             // fill color (none)
        return series ;
    }



    private XYSeries seriesBuilding2(int nP, double jI){
        Number[] array = new Number[nP];
        Number[][] array2 = new Number[nP][2];
        XYSeries series ;
        SimpleXYSeries simpleSeries = null ;

        for (int j=0 ; j<=nP-1 ; j++)   {
            simpleSeries.addLast(1d,1d);
//            simpleSeries.addLast(randomDouble(),randomDouble());
        }
        series = simpleSeries;
//        series = new SimpleXYSeries(
//                Arrays.asList(array),          // SimpleXYSeries takes a List so turn our array into a List
//                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
//                " ");                             // fill color (none)
        return series ;
    }


    private XYSeries seriesBuilding3(int nP, double jI){
        Number[] arrayX = new Number[nP];
        Number[] arrayY = new Number[nP];
        SimpleXYSeries simpleSeries1 ;
        XYSeries series ;
        for (int j=0 ; j<=nP-1 ; j++)   {
            arrayX[j] = Math.cos(0.1d* j + jI) ;
            arrayY[j] = Math.sin(0.1d* j + jI) ;
        }
        simpleSeries1 = new SimpleXYSeries(
                Arrays.asList(arrayX),          // SimpleXYSeries takes a List so turn our array into a List
                Arrays.asList(arrayY),          // SimpleXYSeries takes a List so turn our array into a List
//                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                " ");                             // fill color (none)

//---        series = new SimpleXYSeries(
//---                Arrays.asList(arrayX),          // SimpleXYSeries takes a List so turn our array into a List
//---                Arrays.asList(arrayY),          // SimpleXYSeries takes a List so turn our array into a List
//                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
//---                " ");                             // fill color (none)
        simpleSeries1.addLast(0d,0d);
        simpleSeries1.addLast(2d,2d);
        simpleSeries1.addLast(-2d,2d);
        simpleSeries1.addLast(2d,-2d);
        simpleSeries1.addLast(-2d,-2d);
        series =simpleSeries1 ;
        return series ;
    }


    private XYSeries seriesBuilding4(int nP, double jI){
        Number[] arrayX = null;
        Number[] arrayY = null;
        SimpleXYSeries simpleSeries1 ;
        XYSeries series1 ;
        double varX, varY, tempVar ;

        simpleSeries1 = new SimpleXYSeries(" ");

//        for (int j=0 ; j<=nP-1 ; j++)   {
//            arrayX[j] = Math.cos(0.1d* j + jI) ;
//            arrayY[j] = Math.sin(0.1d* j + jI) ;
//        }

//---        series = new SimpleXYSeries(
//---                Arrays.asList(arrayX),          // SimpleXYSeries takes a List so turn our array into a List
//---                Arrays.asList(arrayY),          // SimpleXYSeries takes a List so turn our array into a List
//                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
//---                " ");                             // fill color (none)


        for (int j=0 ; j<=nP-1 ; j++)   {
            varX = randomDouble();
            tempVar = randomDouble();
            varY = randomDouble();
//                simpleSeries1.addLast(varX,varY);
            simpleSeries1.addLast(varX,varY);
        }

        series1 =simpleSeries1 ;
        return series1 ;
    }


    private XYSeries seriesBuilding5(int nP, double jI){
        Number[] array = new Number[nP];
        XYSeries series ;
        for (int j=0 ; j<=nP-1 ; j++)   {
            array[j] = randomDouble() ;
        }
        series = new SimpleXYSeries(
                Arrays.asList(array),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                " ");                             // fill color (none)
        return series ;
    }



    private static double randomDouble() {
//        Random azar = new Random();
//        return azar.nextDouble();
        double azar = Math.random();
        return azar;
    }



}
